// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString, numericLiterals, unnecessaryImport
/**
 * Package domain
 */
package teamrocket.domain; // $codepro.audit.disable packagePrefixNamingConvention -->

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;

/**
 * This class is will generate a full PatientHealthHistory
 * chart given a Patient object.  It writes the history 
 * report out to disk as healthreport.jpg.  
 * @author William Barr
 *
 * @version $Revision: 1.0 $
 */
public class PatientChart implements IReport {

	
	/**
	 * Method generate.
	 * @param params HashMap<String,Object>
	 * @see teamrocket.domain.IReport#generate(HashMap<String,Object>)
	 */
	@Override
	public void generate(HashMap<String, Object> params) { // $codepro.audit.disable declareAsInterface, spellCheckIdentifiers
		final Patient patient = (Patient) params.get("patient");
		
		final Collection<TreatmentRecord> records = 
			TreatmentRecord.getTreatmentRecords(patient.getPatientID());
		
		final TimeSeries dataSeries = new TimeSeries("Weight");
		for(TreatmentRecord eachRecord : records){
			Day recordDate =new Day(eachRecord.getDate());
			float weight = eachRecord.getVitalSigns().getWeight();
			TimeSeriesDataItem dataPoint = new TimeSeriesDataItem(
					recordDate, (double) weight);
			
			dataSeries.add(dataPoint);
			
		}
		final TimeSeriesCollection collection = new TimeSeriesCollection(dataSeries);
		final JFreeChart theChart = ChartFactory.createTimeSeriesChart(
				"Patient Health History", "Date", "Weight (lbs)", collection, 
				true, true, false);
		
		try {
			ChartUtilities.saveChartAsJPEG(
					new File("healthreport.jpg"), theChart, 344, 284);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
